<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<!-- <script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script> -->
<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.core.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.widget.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.position.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>
<style>
#txtHint{position:absolute; top:260px; left:585px; display:none; background-color: #EDFAF4;min-width:281px;  border: 1px solid #aaa; border-radius:5px;height:150px;overflow-x:hidden;}
#txtHint li{list-style:none; color:#333;border:1px solid #EDFAF4;padding: 0px 8px;}
#txtHint li:hover{color:#c37100; background-color:#fdf8d5; border:1px solid #fac900; border-radius:25px;}
#detail span:hover {color: #c37100;}
#group {position: relative;}
.table1 thead tr th, .table1 tbody tr td {text-align: center;}
</style>

<!--显示已采购未待收货的采购单查询<?php echo URL::site('unreceipt/sku_select'); ?>-->
<form action="<?php echo URL::base(); ?>unreceipt/sku_select" method="get" class="form-horizontal">
    <div class="control-group">
        <label class="control-label"> SKU:</label>
        <div class="controls">
        <input type="text" value="<?php if (isset($sku)) {echo $sku;} ?>" name="sku" id="sku"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">计划员:</label>
        <div class="controls">
        <input name="operate_man" id="operate_man" type="text"
             value="<?php if (isset($operate_man)) {echo $operate_man;} ?>"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label"> 跟单员:</label>
        <div class="controls">
        <input name="merchandiser" id="merchandiser" type="text"
             value="<?php if (isset($merchandiser)) {echo $merchandiser;} ?>"/>
        </div>
    </div>

    <div class="control-group" id="group">
    	<label class="control-label"> 供应商:</label>
        <div class="controls">
    	 <input type="hidden" class="input-mini" name="supplier_id" id="supplier_id" 
    	 	value="<?php echo $supplier_id;?>" autocomplete="off" readonly="readonly"/>
        <input type="text" name="supplier_name" id="supplier_name"
        	value="<?php echo $supplier_name;?>" placeholder="请输入供应商" autocomplete="off"/>
        <a href="javascript:clear();" class="btn btn-default">重选</a>
        </div>
    </div>    
    <div class="control-group"><label class="control-label"> 预约单日期:</label>
        <div class="controls">
        <input class="input-medium" type="text"  name="date_from_c" id="c_begintime"
            value="<?php if (isset($date_from_c)) {echo $date_from_c;} ?>"/>
       	 至
        <input class="input-medium" name="date_to_c" type="text" id="c_endtime"
        	value="<?php if (isset($date_to_c)) {echo $date_to_c;} ?>"/>
        </div>
    </div>
    <div class="control-group">
        <div class="controls">
        <input type="submit" class="btn btn-info" id="sku_select" value="查询"/>
        <input type="button" class="btn btn-default" id="download" value="导出"/>
        </div>
    </div>
</form>
<!-- 显示根据各种条件查询出来的采购单的信息 -->
	<?php if (isset($sku_select)) {
    ?>
    <table class="table table-hover">
        <thead>
        <tr>
            <th>VMI预约单号</th>
            <th>SKU</th>
            <th>中文名称 </th>
            <th>规格</th>
            <th>储位</th>
            <th>供应商</th>
            <th>计划员</th>
            <th>跟单员</th>
            <th>采购数量</th>
            <th>预约单时间</th>
            <th>明细单状态</th>
            <th>已收货</th>
            <th>已入库</th>
            <th>退回</th>
            <th>备注</th>
        </tr>
        </thead>
        <tbody>
        <?php
            foreach ($sku_select as $v) {
        ?>
            <tr>
                <!-- 采购单号 -->
                <td>
                    <?php echo $v['purchaseorder_id']; ?>
                </td>
                <!-- SKU -->
                <td>
                    <?php echo $v['sku']; ?>
                </td>
                <!-- SKU名称 -->
                <td>
                    <?php echo $v['sku_name']; ?>
                </td>
                <!-- SKU规格 -->
                <td>
                    <?php echo $v['sku_standard']; ?>
                </td>
                <!-- SKU储位 -->
                <td>
                    <?php echo $v['storage_position']; ?>
                </td>
                <!-- 供应商 -->
                <td>
                    <?php echo supplierskus::getNameByIdForVmi($v['supplier_id']);?>
                </td>
                <!-- 计划员 -->
                <td>
                    <?php echo Erpuser::nick_by_id($v['purchaseplaner']);?>
                </td>
                <!-- 跟单员 -->
                <td><?php echo $v['merchandiser'];?></td>
                <!-- 采购数量 -->
                <td>
                    <?php echo $v['quantity']; ?>
                </td>
                <!-- 采购时间 -->
                <td>
                    <?php echo $v['purchase_time']; ?>
                </td>
                <td>
                    <?php echo $v['pod_status']; ?>
                </td>
                <td>
                    <?php echo $v['recieve_quantity']; ?>
                </td>
                <td>
                    <?php echo $v['ware_quantity']; ?>
                </td>
                <td>
                    <?php echo $v['return_quantity']; ?>
                </td>
                <td><?php echo $v['remark']; ?></td>
            </tr>
        <?php } ?></tbody>
    </table>
    <?php
    if (isset($page_links)) {
        echo $page_links;
    }
} ?>
<script type="text/javascript">
    $(document).ready(function () {
        laydate.render({
            elem: '#c_begintime'
        });
        laydate.render({
            elem: '#c_endtime'
        });
        $('.table1 > tbody >tr:nth-child(odd)').css("background-color", "#f5f5f5");
        //当输入的到货数量大于采购数量的时候给予提示
        $(".focuss").blur(function () {
            var quantity = $(this).attr("id");
            var quantity_count = $(this).val();
            var leng = quantity.length;//长度
            var id = quantity.substr(1, leng - 1);//采购明细单号
            var purchase_count = $("#purchase_count_" + id).val();//采购数量
            var arrived = $("#span_" + id).text();//已经到货的采购数量
            var subtract = purchase_count - quantity_count - arrived;

            if (subtract > 0) {
                alert("你填写的数字比采购数量小" + subtract);
            }
            else if (subtract == 0) {
                alert("你填写的数字加上已经到货数量等于采购数量");
            }
            else {
                alert("你填写的数字大于采购数量");
            }
        });
        $("#supplier_id").keyup(function (e) {
            var $val = $(this).val();
            showHint($val);
        });

        var all_val = new Array();
        var input_values = $(".test_input");
        for (var n = 0; n < input_values.length; n++) {
            var pre = "arrival_quantity_";
            var id = input_values[n].id.substr(pre.length);
            var quantity = $("#arrival_quantity_" + id).val();
            var data = {"id": id, "quantity": quantity};
            all_val.push(data);
        }
        $(".myupdate").attr('disabled', "true");
        $(".test_input").focus(function () {
            var pre = "arrival_quantity_";
            var id = $(this).attr('id').substr(pre.length);
            var before = $("#jiu_" + id).val();

            $(this).keyup(function () {
                var after = $(this).val().replace(/[ ]/g, "");
                $(this).val(after);

                if (before != after) {
                    $("#up_" + id).removeAttr('disabled');
                }
                else {
                    $("#up_" + id).attr('disabled', "true");
                }

            })
        });

        $(".change_number").blur(function () {
            var ids = $(this).attr("id");//修改按钮的ID
            var lengs = ids.length;
            var id = ids.substr(17, lengs - 17);//采购明细的ID
            var sku = $("#sku_" + id).val();
            var jiu = $("#jiu_" + id).val();
            var arrival_quantity = $("#arrival_quantity_" + id).val();
            var val = arrival_quantity - jiu;//修改后的到货数量减去之前的到货数量
            if (val < 0) {
                alert("您修改的数字比修改前的数字小" + val);
                return;
            }else {
                alert("您修改的数字比修改前的数字大" + val);
                return;
            }
        });
        $(".myupdate").click(function () {
            var ids = $(this).attr("id");//修改按钮的ID
            var lengs = ids.length;
            var id = ids.substr(3, lengs - 3);//产品的ID
            var sku = $("#sku_" + id).val();
            var jiu = $("#jiu_" + id).val();
            var arrival_quantity = $("#arrival_quantity_" + id).val();
            var purchaseorder = $("#purchaseorder").val();
            var val = arrival_quantity - jiu;//修改后的到货数量减去之前的到货数量

            $.ajax({
                type: 'post',
                url: '<?php echo url::base(TRUE);?>receipt/receipt_update',
                dataType: 'html',
                data: "id=" + id + "&sku=" + sku + "&purchaseorder=" + purchaseorder + "&arrival_quantity=" + arrival_quantity + "&count_val=" + val,
                async: false,
                success: function (data) {
                    alert(data);
                    window.location.href = window.location.href;
                    window.location.reload;
                },
                error: function () {
                    alert("error");
                }
            });
        });

        $("#submit_id").click(function () {
            $(this).attr('disabled', true);
            $(".upSuppForm")[0].submit()
        });
    })
    $('#download').click(function () {
            var sku = $('#sku').val();
            var operate_man = $('#operate_man').val();
            var supplier = $('#supplier_id').val();
            var date_from = $('#c_begintime').val();
            var date_to = $('#c_endtime').val();
            window.location.href = "<?php echo URL::base() ?>unreceipt/export?sku=" + sku + "&operate_man=" + operate_man + "&supplier_id="
                + supplier + "&date_from=" + date_from + "&date_to=" + date_to;
        }
    );

    function showHint(str) {
        if (str.length == 0) {
            $("#supplier_id").val('');
            $("#txtHint").hide();
            return false;
        }
        var url = "<?php echo URL::base() ?>purchase/get_supplier";
        url += "?term=" + str;
        url += "&sid=" + Math.random();
        $.getJSON(url, function (data) {
            $("#txtHint").html("");
            if(data.length){
                $.each(data, function (index, value) {
                    $('<li>' + value.name + '</li>').appendTo($("#txtHint"));
                    $("#txtHint").show();
                    $('#txtHint li').click(function () {
                        $("#supplier_id").val($(this).text());
                        $(this).parent().hide();
                    });
                });
            }
        });
    }
    /**
     * 查询供应商自动提示补全
     */
    $("#supplier_name").autocomplete({
        autoFocus: true,
        minLength: 0,
        autoFill: true,
        source: '<?php echo URL::base(true)?>purchase/get_supplier',
        select: function (event, ui) {
            //从建议列表中选中之后触发的事件
            $("#supplier_name").val(ui.item.name);
            $("#supplier_id").val(ui.item.id);
            $("#supplier_name").attr("readonly", "readonly");
            return false;
        }
    });
    $("#supplier_name").blur(function(){
        $.getJSON('<?php echo URL::base(true)?>purchase/get_supplier', {'term':$(this).val()}, function(data){
            if($.isArray(data) && typeof(data[0]) == 'object') {
                $("#supplier_name").val(data[0].name);
                $("#supplier_id").val(data[0].id);
                $("#supplier_name").attr("readonly", "readonly");
            }
        });
    });
    function clear(e){
        var e = e || window.event;
        $("#supplier_id").val('');
        $("#supplier_name").val('');
        $("#supplier_name").attr("readonly", false);
        if(typeof(e) == 'undefined'){
			return ;
        }else{
        	e.preventDefault && e.preventDefault();
        }
    }
</script>